<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-US">
<head>
<!-- GenHTML revision 25226-->
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Using Custom Objects - The Java EE 6 Tutorial</title>
<meta name="robots" content="index,follow">
<meta name="robots" content="index,follow">
<meta name="date" content="2011-03-01">
<link rel="stylesheet" type="text/css" href="css/default.css">
<link rel="stylesheet" type="text/css" href="css/ipg.css">
<link rel="stylesheet" type="text/css" href="css/javaeetutorial.css">
</head>

<body>

<table border="0" cellpadding="5" cellspacing="0" width="100%">
<tbody>
   <tr valign="top">
      <td width="400px"><p class="toc level1"><a href="docinfo.html">Document Information</a></p>
<p class="toc level1 tocsp"><a href="gexaf.html">Preface</a></p>
<p class="toc level1 tocsp"><a href="gfirp.html">Part&nbsp;I&nbsp;Introduction</a></p>
<p class="toc level2"><a href="bnaaw.html">1.&nbsp;&nbsp;Overview</a></p>
<p class="toc level2"><a href="gfiud.html">2.&nbsp;&nbsp;Using the Tutorial Examples</a></p>
<p class="toc level1 tocsp"><a href="bnadp.html">Part&nbsp;II&nbsp;The Web Tier</a></p>
<p class="toc level2"><a href="bnadr.html">3.&nbsp;&nbsp;Getting Started with Web Applications</a></p>
<p class="toc level2"><a href="bnaph.html">4.&nbsp;&nbsp;JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="giepx.html">5.&nbsp;&nbsp;Introduction to Facelets</a></p>
<p class="toc level2"><a href="gjddd.html">6.&nbsp;&nbsp;Expression Language</a></p>
<p class="toc level2"><a href="bnaqz.html">7.&nbsp;&nbsp;Using JavaServer Faces Technology in Web Pages</a></p>
<p class="toc level2"><a href="gjcut.html">8.&nbsp;&nbsp;Using Converters, Listeners, and Validators</a></p>
<p class="toc level2"><a href="bnatx.html">9.&nbsp;&nbsp;Developing with JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="gkmaa.html">10.&nbsp;&nbsp;JavaServer Faces Technology Advanced Concepts</a></p>
<p class="toc level2"><a href="bnawo.html">11.&nbsp;&nbsp;Configuring JavaServer Faces Applications</a></p>
<p class="toc level2"><a href="gkiow.html">12.&nbsp;&nbsp;Using Ajax with JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="gkhxa.html">13.&nbsp;&nbsp;Advanced Composite Components</a></p>
<p class="toc level2"><a href="bnavg.html">14.&nbsp;&nbsp;Creating Custom UI Components</a></p>
<p class="toc level3"><a href="bnavh.html">Determining Whether You Need a Custom Component or Renderer</a></p>
<p class="toc level4"><a href="bnavh.html#bnavi">When to Use a Custom Component</a></p>
<p class="toc level4"><a href="bnavh.html#bnavj">When to Use a Custom Renderer</a></p>
<p class="toc level4"><a href="bnavh.html#bnavk">Component, Renderer, and Tag Combinations</a></p>
<p class="toc level3 tocsp"><a href="bnavt.html">Steps for Creating a Custom Component</a></p>
<p class="toc level3"><a href="bnavu.html">Creating Custom Component Classes</a></p>
<p class="toc level4"><a href="bnavu.html#bnavv">Specifying the Component Family</a></p>
<p class="toc level4"><a href="bnavu.html#bnavw">Performing Encoding</a></p>
<p class="toc level4"><a href="bnavu.html#bnavx">Performing Decoding</a></p>
<p class="toc level4"><a href="bnavu.html#bnavy">Enabling Component Properties to Accept Expressions</a></p>
<p class="toc level4"><a href="bnavu.html#bnavz">Saving and Restoring State</a></p>
<p class="toc level3 tocsp"><a href="bnawa.html">Delegating Rendering to a Renderer</a></p>
<p class="toc level4"><a href="bnawa.html#bnawb">Creating the Renderer Class</a></p>
<p class="toc level4"><a href="bnawa.html#bnawc">Identifying the Renderer Type</a></p>
<p class="toc level3 tocsp"><a href="bnawd.html">Handling Events for Custom Components</a></p>
<p class="toc level3"><a href="bnawe.html">Creating the Component Tag Handler</a></p>
<p class="toc level4"><a href="bnawe.html#bnawf">Retrieving the Component Type</a></p>
<p class="toc level4"><a href="bnawe.html#bnawg">Setting Component Property Values</a></p>
<p class="toc level5"><a href="bnawe.html#bnawh">Getting the Attribute Values</a></p>
<p class="toc level5"><a href="bnawe.html#bnawi">Setting the Component Property Values</a></p>
<p class="toc level4 tocsp"><a href="bnawe.html#bnawl">Providing the Renderer Type</a></p>
<p class="toc level4"><a href="bnawe.html#bnawm">Releasing Resources</a></p>
<p class="toc level3 tocsp"><a href="bnawn.html">Defining the Custom Component Tag in a Tag Library Descriptor</a></p>
<p class="toc level3"><a href="bnaus.html">Creating a Custom Converter</a></p>
<p class="toc level3"><a href="bnaut.html">Implementing an Event Listener</a></p>
<p class="toc level4"><a href="bnaut.html#bnauu">Implementing Value-Change Listeners</a></p>
<p class="toc level4"><a href="bnaut.html#bnauv">Implementing Action Listeners</a></p>
<p class="toc level3 tocsp"><a href="bnauw.html">Creating a Custom Validator</a></p>
<p class="toc level4"><a href="bnauw.html#bnaux">Implementing the Validator Interface</a></p>
<p class="toc level4"><a href="bnauw.html#bnauy">Creating a Custom Tag</a></p>
<p class="toc level5"><a href="bnauw.html#bnauz">Writing the Tag Handler</a></p>
<p class="toc level5"><a href="bnauw.html#bnava">Writing the Tag Library Descriptor</a></p>
<div id="scrolltoc" class="onpage">
<p class="toc level3 tocsp"><a href="">Using Custom Objects</a></p>
<p class="toc level4"><a href="#bnatu">Using a Custom Converter</a></p>
<p class="toc level4"><a href="#bnatv">Using a Custom Validator</a></p>
<p class="toc level4"><a href="#bnatw">Using a Custom Component</a></p>
</div>
<p class="toc level3 tocsp"><a href="bnatg.html">Binding Component Values and Instances to External Data Sources</a></p>
<p class="toc level4"><a href="bnatg.html#bnati">Binding a Component Value to a Property</a></p>
<p class="toc level4"><a href="bnatg.html#bnatj">Binding a Component Value to an Implicit Object</a></p>
<p class="toc level4"><a href="bnatg.html#bnatl">Binding a Component Instance to a Bean Property</a></p>
<p class="toc level3 tocsp"><a href="bnatm.html">Binding Converters, Listeners, and Validators to Managed Bean Properties</a></p>
<p class="toc level2 tocsp"><a href="bnafd.html">15.&nbsp;&nbsp;Java Servlet Technology</a></p>
<p class="toc level2"><a href="bnaxu.html">16.&nbsp;&nbsp;Internationalizing and Localizing Web Applications</a></p>
<p class="toc level1 tocsp"><a href="bnayk.html">Part&nbsp;III&nbsp;Web Services</a></p>
<p class="toc level2"><a href="gijti.html">17.&nbsp;&nbsp;Introduction to Web Services</a></p>
<p class="toc level2"><a href="bnayl.html">18.&nbsp;&nbsp;Building Web Services with JAX-WS</a></p>
<p class="toc level2"><a href="giepu.html">19.&nbsp;&nbsp;Building RESTful Web Services with JAX-RS</a></p>
<p class="toc level2"><a href="gjjxe.html">20.&nbsp;&nbsp;Advanced JAX-RS Features</a></p>
<p class="toc level2"><a href="gkojl.html">21.&nbsp;&nbsp;Running the Advanced JAX-RS Example Application</a></p>
<p class="toc level1 tocsp"><a href="bnblr.html">Part&nbsp;IV&nbsp;Enterprise Beans</a></p>
<p class="toc level2"><a href="gijsz.html">22.&nbsp;&nbsp;Enterprise Beans</a></p>
<p class="toc level2"><a href="gijre.html">23.&nbsp;&nbsp;Getting Started with Enterprise Beans</a></p>
<p class="toc level2"><a href="gijrb.html">24.&nbsp;&nbsp;Running the Enterprise Bean Examples</a></p>
<p class="toc level2"><a href="bnbpk.html">25.&nbsp;&nbsp;A Message-Driven Bean Example</a></p>
<p class="toc level2"><a href="gkcqz.html">26.&nbsp;&nbsp;Using the Embedded Enterprise Bean Container</a></p>
<p class="toc level2"><a href="gkidz.html">27.&nbsp;&nbsp;Using Asynchronous Method Invocation in Session Beans</a></p>
<p class="toc level1 tocsp"><a href="gjbnr.html">Part&nbsp;V&nbsp;Contexts and Dependency Injection for the Java EE Platform</a></p>
<p class="toc level2"><a href="giwhb.html">28.&nbsp;&nbsp;Introduction to Contexts and Dependency Injection for the Java EE Platform</a></p>
<p class="toc level2"><a href="gjbls.html">29.&nbsp;&nbsp;Running the Basic Contexts and Dependency Injection Examples</a></p>
<p class="toc level2"><a href="gjehi.html">30.&nbsp;&nbsp;Contexts and Dependency Injection for the Java EE Platform: Advanced Topics</a></p>
<p class="toc level2"><a href="gkhre.html">31.&nbsp;&nbsp;Running the Advanced Contexts and Dependency Injection Examples</a></p>
<p class="toc level1 tocsp"><a href="bnbpy.html">Part&nbsp;VI&nbsp;Persistence</a></p>
<p class="toc level2"><a href="bnbpz.html">32.&nbsp;&nbsp;Introduction to the Java Persistence API</a></p>
<p class="toc level2"><a href="gijst.html">33.&nbsp;&nbsp;Running the Persistence Examples</a></p>
<p class="toc level2"><a href="bnbtg.html">34.&nbsp;&nbsp;The Java Persistence Query Language</a></p>
<p class="toc level2"><a href="gjitv.html">35.&nbsp;&nbsp;Using the Criteria API to Create Queries</a></p>
<p class="toc level2"><a href="gkjiq.html">36.&nbsp;&nbsp;Creating and Using String-Based Criteria Queries</a></p>
<p class="toc level2"><a href="gkjjf.html">37.&nbsp;&nbsp;Controlling Concurrent Access to Entity Data with Locking</a></p>
<p class="toc level2"><a href="gkjia.html">38.&nbsp;&nbsp;Improving the Performance of Java Persistence API Applications By Setting a Second-Level Cache</a></p>
<p class="toc level1 tocsp"><a href="gijrp.html">Part&nbsp;VII&nbsp;Security</a></p>
<p class="toc level2"><a href="bnbwj.html">39.&nbsp;&nbsp;Introduction to Security in the Java EE Platform</a></p>
<p class="toc level2"><a href="bncas.html">40.&nbsp;&nbsp;Getting Started Securing Web Applications</a></p>
<p class="toc level2"><a href="bnbyk.html">41.&nbsp;&nbsp;Getting Started Securing Enterprise Applications</a></p>
<p class="toc level1 tocsp"><a href="gijue.html">Part&nbsp;VIII&nbsp;Java EE Supporting Technologies</a></p>
<p class="toc level2"><a href="gijto.html">42.&nbsp;&nbsp;Introduction to Java EE Supporting Technologies</a></p>
<p class="toc level2"><a href="bncih.html">43.&nbsp;&nbsp;Transactions</a></p>
<p class="toc level2"><a href="bncjh.html">44.&nbsp;&nbsp;Resource Connections</a></p>
<p class="toc level2"><a href="bncdq.html">45.&nbsp;&nbsp;Java Message Service Concepts</a></p>
<p class="toc level2"><a href="bncgv.html">46.&nbsp;&nbsp;Java Message Service Examples</a></p>
<p class="toc level2"><a href="gkahp.html">47.&nbsp;&nbsp;Advanced Bean Validation Concepts and Examples</a></p>
<p class="toc level2"><a href="gkeed.html">48.&nbsp;&nbsp;Using Java EE Interceptors</a></p>
<p class="toc level1 tocsp"><a href="gkgjw.html">Part&nbsp;IX&nbsp;Case Studies</a></p>
<p class="toc level2"><a href="gkaee.html">49.&nbsp;&nbsp;Duke's Tutoring Case Study Example</a></p>
<p class="toc level1 tocsp"><a href="idx-1.html">Index</a></p>
</td>
      <td width="10px">&nbsp;</td>
      <td>
         <div class="header">
             <div class="banner">
                <table width="100%" border="0" cellpadding="5" cellspacing="0">
                   <tbody>
                      <tr>
                         <td valign="bottom"><p class="Banner">The Java EE 6 Tutorial
</p></td>
                         <td align="right"  valign="bottom"><img src="graphics/javalogo.png" alt="Java Coffee Cup logo"></td>
                      </tr>
                   </tbody>
                </table>
             </div>

             <div class="header-links">
	         <a href="./index.html">Home</a> | 
<a href="../information/download.html">Download</a> | 
<a href="./javaeetutorial6.pdf">PDF</a> | 
<a href="../information/faq.html">FAQ</a> | 
<a href="http://download.oracle.com/javaee/feedback.htm">Feedback</a>

             </div>
             <div class="navigation">
                 <a href="bnauw.html"><img src="graphics/leftButton.gif" border="0" alt="Previous" title="Previous"></a>
                 <a href="p1.html"><img src="graphics/upButton.gif" border="0" alt="Contents" title="Contents"></a>
                 <a href="bnatg.html"><img src="graphics/rightButton.gif" border="0" alt="Next" title="Next"></a>
             </div>
         </div>

	 <div class="maincontent">      	 
             

<a name="bnatt"></a><h2>Using Custom Objects</h2>
<a name="indexterm-1005"></a><p>As a page author, you might need to use custom converters, validators, or
components packaged with the application on your Facelets pages.</p>

<p><a name="indexterm-1006"></a><a name="indexterm-1007"></a>A custom converter is applied to a component in one of the
following ways:</p>


<ul><li><p><a name="indexterm-1008"></a><a name="indexterm-1009"></a>Reference the converter from the component tag&rsquo;s <tt>converter</tt> attribute.</p>

</li>
<li><p><a name="indexterm-1010"></a><a name="indexterm-1011"></a>Nest a <tt>converter</tt> tag inside the component&rsquo;s tag and reference the custom converter from one of the <tt>converter</tt> tag&rsquo;s attributes.</p>

</li></ul>
<p><a name="indexterm-1012"></a><a name="indexterm-1013"></a>A custom validator is applied to a component in one of the
following ways:</p>


<ul><li><p><a name="indexterm-1014"></a><a name="indexterm-1015"></a>Nest a <tt>validator</tt> tag inside the component&rsquo;s tag and reference the custom validator from the <tt>validator</tt> tag.</p>

</li>
<li><p>Nest the validator&rsquo;s custom tag (if there is one) inside the component&rsquo;s tag.</p>

</li></ul>
<p><a name="indexterm-1016"></a><a name="indexterm-1017"></a><a name="indexterm-1018"></a>To use a custom component, you add the custom tag associated with the
component to the page.</p>

<p>As explained in <a href="bnarb.html">Setting Up a Page</a>, you must ensure that the TLD that defines any
custom tags is packaged in the application if you intend to use the
tags in your pages. TLD files are stored in the <tt>WEB-INF/</tt> directory or
subdirectory of the WAR file or in the <tt>META-INF/</tt> directory or subdirectory of
a tag library packaged in a JAR file.</p>

<p>The next three sections describe how to use custom converter, validator, and UI
components.</p>



<a name="bnatu"></a><h3>Using a Custom Converter</h3>
<a name="indexterm-1019"></a><a name="indexterm-1020"></a><a name="indexterm-1021"></a><p><a name="indexterm-1022"></a><a name="indexterm-1023"></a>As described in the previous section, to apply the data conversion performed by
a custom converter to a particular component&rsquo;s value, you must either reference the
custom converter from the component tag&rsquo;s <tt>converter</tt> attribute or from a <tt>converter</tt>
tag nested inside the component tag.</p>

<p>If you are using the component tag&rsquo;s <tt>converter</tt> attribute, this attribute must reference
the <tt>Converter</tt> implementation&rsquo;s identifier or the fully-qualified class name of the converter. <a href="bnaus.html">Creating a Custom Converter</a>
explains how a custom converter is implemented.</p>

<p>The identifier for the credit card converter is <tt>CreditCardConverter</tt>. The <tt>CreditCardConverter</tt> instance
is registered on the <tt>ccno</tt> component, as shown in the following example:</p>

<pre>&lt;h:inputText id="ccno"
    size="19"
    converter="CreditCardConverter"
    required="true">
    ...
&lt;/h:inputText></pre><p><a name="indexterm-1024"></a>By setting the <tt>converter</tt> attribute of a component&rsquo;s tag to the converter&rsquo;s
identifier or its class name, you cause that component&rsquo;s local value to be
automatically converted according to the rules specified in the <tt>Converter</tt> implementation.</p>

<p>Instead of referencing the converter from the component tag&rsquo;s <tt>converter</tt> attribute, you
can reference the converter from a <tt>f:converter</tt> tag nested inside the component&rsquo;s
tag. To reference the custom converter using the <tt>converter</tt> tag, you do one
of the following:</p>


<ul><li><p>Set the <tt>f:converter</tt> tag&rsquo;s <tt>converterId</tt> attribute to the <tt>Converter</tt> implementation&rsquo;s identifier defined in the <tt>@FacesConverter</tt> annotation or in the application configuration resource file. This method is shown in the following example:</p>

<pre>&lt;h:inputText id="ccno"
    size="19">
    &lt;f:converter converterId="CreditCardConverter"/>
 &lt;/h:inputText></pre></li>
<li><p>Bind the <tt>Converter</tt> implementation to a managed bean property using the <tt>converter</tt> tag&rsquo;s <tt>binding</tt> attribute, as described in <a href="bnatm.html">Binding Converters, Listeners, and Validators to Managed Bean Properties</a>.</p>

</li></ul>


<a name="bnatv"></a><h3>Using a Custom Validator</h3>
<a name="indexterm-1025"></a><a name="indexterm-1026"></a><a name="indexterm-1027"></a><p>To register a custom validator on a component, you must do one
of the following:</p>


<ul><li><p>Nest the validator&rsquo;s custom tag inside the tag of the component whose value you want to be validated.</p>

</li>
<li><p>Nest the standard <tt>validator</tt> tag within the tag of the component and reference the custom <tt>Validator</tt> implementation from the <tt>validator</tt> tag.</p>

</li></ul>
<p>Here is the custom <tt>formatValidator</tt> tag for the Credit Card Number field:</p>

<pre>&lt;h:inputText id="ccno" size="19"
    ...
    required="true">
    &lt;mystore:formatValidator
         formatPatterns="9999999999999999|9999 9999 9999 9999|
        9999-9999-9999-9999" />
&lt;/h:inputText>
&lt;h:message styleClass="validationMessage"  for="ccno"/></pre><p>This tag validates the input of the <tt>ccno</tt> field against the patterns defined
by the page author in the <tt>formatPatterns</tt> attribute.</p>

<p>You can use the same custom validator for any similar component by
simply nesting the custom validator tag within the component tag.</p>

<p><a name="indexterm-1028"></a>If the application developer who created the custom validator prefers to configure the
attributes in the <tt>Validator</tt> implementation rather than allow the page author to configure the
attributes from the page, the developer will not create a custom tag for
use with the validator.</p>

<p>In this case, the page author must nest the <tt>validator</tt> tag inside the
tag of the component whose data needs to be validated. Then the page
author needs to do one of the following:</p>


<ul><li><p>Set the <tt>validator</tt> tag&rsquo;s <tt>validatorId</tt> attribute to the ID of the validator that is defined in the application configuration resource file.</p>

</li>
<li><p>Bind the custom <tt>Validator</tt> implementation to a managed bean property using the <tt>validator</tt> tag&rsquo;s <tt>binding</tt> attribute, as described in <a href="bnatm.html">Binding Converters, Listeners, and Validators to Managed Bean Properties</a>.</p>

</li></ul>
<p>The following tag registers a hypothetical validator on a component using a <tt>validator</tt>
tag and references the ID of the validator:</p>

<pre>&lt;h:inputText id="name" value="#{CustomerBean.name}"
            size="10" ... >
    &lt;f:validator validatorId="customValidator" />
    ...
&lt;/h:inputText></pre>

<a name="bnatw"></a><h3>Using a Custom Component</h3>
<a name="indexterm-1029"></a><p>In order to use a custom component in a page, you need
to declare the tag library that defines the custom tag that renders the
custom component, as explained in <a href="">Using Custom Objects</a>, and you add the component&rsquo;s tag to the
page.</p>

<p>The Duke&rsquo;s Bookstore application includes a custom image map component on the <tt>chooselocale.jsp</tt>
page. This component allows you to select the locale for the application by
clicking on a region of the image map:</p>

<pre>...
&lt;h:graphicImage id="mapImage" url="resources/images/world.jpg"
                alt="#{bundle.chooseLocale}"
                usemap="#worldMap" />
&lt;bookstore:map id="worldMap" current="NAmerica" immediate="true"
               action="bookstore">
    &lt;f:actionListener type="dukesbookstore.listeners.MapLocaleChangeListener" />
    &lt;bookstore:area id="NAmerica" value="#{NA}" 
                    onmouseover="resources/images/world_namer.jpg" 
                    onmouseout="resources/images/world.jpg" 
                    targetImage="mapImage" />
    ...
    &lt;bookstore:area id="France" value="#{fraA}" 
                    onmouseover="resources/images/world_france.jpg" 
                    onmouseout="resources/images/world.jpg" 
                    targetImage="mapImage" />
&lt;/bookstore:map></pre><p>The standard <tt>graphicImage</tt> tag associates an image (<tt>world.jpg</tt>) with an image map that
is referenced in the <tt>usemap</tt> attribute value.</p>

<p>The custom <tt>map</tt> tag that represents the custom component, <tt>MapComponent</tt>, specifies the
image map, and contains a set of <tt>area</tt> tags. Each custom <tt>area</tt>
tag represents a custom <tt>AreaComponent</tt> and specifies a region of the image
map.</p>

<p>On the page, the <tt>onmouseover</tt> and <tt>onmouseout</tt> attributes specify the image that is
displayed when the user performs the actions described by the attributes. The page
author defines what these images are. The custom renderer also renders an <tt>onclick</tt>
attribute.</p>

<p>In the rendered HTML page, the <tt>onmouseover</tt>, <tt>onmouseout</tt>, and <tt>onclick</tt> attributes define
which JavaScript code is executed when these events occur. When the user moves
the mouse over a region, the <tt>onmouseover</tt> function associated with the region displays the
map with that region highlighted. When the user moves the mouse out of
a region, the <tt>onmouseout</tt> function redisplays the original image. When the user clicks
a region, the <tt>onclick</tt> function sets the value of a hidden <tt>input</tt> tag
to the ID of the selected area and submits the page.</p>

<p>When the custom renderer renders these attributes in HTML, it also renders the
JavaScript code. The custom renderer also renders the entire <tt>onclick</tt> attribute rather
than let the page author set it.</p>

<p><a name="indexterm-1030"></a>The custom renderer that renders the <tt>map</tt> tag also renders a hidden <tt>input</tt>
component that holds the current area. The server-side objects retrieve the value of
the hidden <tt>input</tt> field and set the locale in the <tt>FacesContext</tt> instance according to
which region was selected.</p>


         </div>
         <div class="navigation">
             <a href="bnauw.html"><img src="graphics/leftButton.gif" border="0" alt="Previous" title="Previous"></a>
             <a href="p1.html"><img src="graphics/upButton.gif" border="0" alt="Contents" title="Contents"></a>
             <a href="bnatg.html"><img src="graphics/rightButton.gif" border="0" alt="Next" title="Next"></a>
         </div>

         <div class="copyright">
      	    <p>Copyright &copy; 2011, Oracle and/or its affiliates. All rights reserved. <a href="docinfo.html">Legal Notices</a></p>
      	 </div>

      </td>
   </tr>
</tbody>
</table>
</body>
</html>

